package com.zzg.service.Impl;

import com.zzg.entity.*;
import com.zzg.mapper.JurisdictionMapper;
import com.zzg.service.JurisdictionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzg
 * @version 1.0.0
 * @ClassName：JurisdictionServiceImpl
 * @Description: 权限管理服务层实现类
 * @date 2021/6/18
 **/
@Service
public class JurisdictionServiceImpl implements JurisdictionService {
    /**
     * dao 权限管理的dao层
     */
    @Resource
    private JurisdictionMapper jurisdiction;


    /**
     * 方法描述
     * @author zzg
     * @date 2021/6/18 11:35
     * @params *@params [userName] 账户信息
     * @return java.util.List<com.zzg.com.zzg.entity.Menu>
     */
    @Override
    public List<Menu> showMenu(String userName) {
        return jurisdiction.showMenu(userName);
    }


    @Override
    public Map<String, Object> roleList(Integer page, Integer limit) {
        //创建一个map集合
        Map<String, Object> map=new HashMap();
        //得到查询结果
        List<Object> objects = jurisdiction.roleList(page, limit);
        //获取查询结果的对象集合
        List<Role>  roleList= (List<Role>) objects.get(0);
        //获取数据总数
        Integer count = ((List<Integer>) objects.get(1)).get(0);
        //添加值
        map.put("code","0");
        map.put("msg","0");
        map.put("count",count);
        map.put("data",roleList);
        return map;
    }

    @Override
    public Map<String, Object> tmsUserList(Integer page, Integer limit) {
        //创建一个map集合
        Map<String, Object> map=new HashMap();
        //得到查询结果
        List<Object> objects = jurisdiction.tmsUserList(page, limit);
        //获取查询结果的对象集合
        List<TmsUser>  roleList= (List<TmsUser>) objects.get(0);
        //获取数据总数
        Integer count = ((List<Integer>) objects.get(1)).get(0);
        //添加值
        map.put("code","0");
        map.put("msg","0");
        map.put("count",count);
        map.put("data",roleList);
        return map;
    }

    /**
     * 菜单添加数据查询所有菜单
     * @author zzg
     * @date 2021/6/21 9:57
     * @params *@params
     * @return
     */
    @Override
    public List<Menu> previousMenu() {
        return jurisdiction.previousMenu();
    }

    @Override
    public int addMenutble(Menu menu) {
        return jurisdiction.addMenutble(menu);
    }

    @Override
    public String delMenu(String menuId) {
        int i = jurisdiction.delMenu(menuId);
        return (i !=1 ) ? "删除失败" : "删除成功";
    }

    @Override
    public Menu showUpdateMenu(String menuId) {
        return jurisdiction.showUpdateMenu(menuId);
    }

    @Override
    public String updateMenu(Menu menu) {
        int i = jurisdiction.updateMenu(menu);
        return (i >=1 ) ? "修改成功" : "修改失败";
    }

    /**
     * 方法描述
     * @author zzg
     * @date 2021/6/18 17:16
     * @params *@params page条 limit页码
     * @return
     */
    @Override
    public Map<String, Object> menuList(Integer page, Integer limit) {
        //创建一个map集合
        Map<String, Object> map=new HashMap();
        //得到查询结果
        List<Object> objects = jurisdiction.menuList(page, limit);
        //获取查询结果的对象集合
        List<Menu>  roleList= (List<Menu>) objects.get(0);
        //获取数据总数
        Integer count = ((List<Integer>) objects.get(1)).get(0);
        //添加值
        map.put("code","0");
        map.put("msg","0");
        map.put("count",count);
        map.put("data",roleList);
        return map;
    }
    @Override
    public Map<String, Object> selectStaff(Integer page, Integer limit) {
        //创建一个map集合
        Map<String, Object> map=new HashMap();
        //得到查询结果
        List<Object> objects = jurisdiction.selectStaff(page, limit);
        //获取查询结果的对象集合
        List<Personnel>  roleList= (List<Personnel>) objects.get(0);
        //获取数据总数
        Integer count = ((List<Integer>) objects.get(1)).get(0);
        //添加值
        map.put("code","0");
        map.put("msg","0");
        map.put("count",count);
        map.put("data",roleList);
        return map;
    }

    @Override
    public int deletePersonner(String id) {
        return jurisdiction.deletePersonner(id);
    }

    @Override
    public int insertTmsuser(TmsUser user) {
        return jurisdiction.insertTmsuser(user);
    }

    @Override
    public int insertPersonner(Personnel per) {
        return jurisdiction.insertPersonner(per);
    }


    @Override
    public void addRole(List<RoleMenu> roleMenus, Role role) {
        int i = jurisdiction.addRole(role);
        int i1 = jurisdiction.addRoleMenu(roleMenus);
    }

    @Override
    public void updateRole(List<RoleMenu> roleMenus, Role role) {
        jurisdiction.delRole(role.getRoleId());
        int i1 = jurisdiction.updateRole(role);
        jurisdiction.addRoleMenu(roleMenus);
    }
    @Override
    public String delRole(String roleId) {
        int i = jurisdiction.delRoleMenu(roleId);
        int i1 = jurisdiction.delRole(roleId);
        return (i >=1 ) && (i1 >=1 )  ? "修改成功" : "修改失败";
    }

    @Override
    public Role ShowUpdateRole(String roleId) {
        return jurisdiction.ShowUpdateRole(roleId);
    }

    @Override
    public TmsUser updateUser(String tmsId) {
        TmsUser tmsUser = jurisdiction.tmsUser(tmsId);
        tmsUser.setRoleids(jurisdiction.selectUserRole(tmsId));
        return tmsUser;
    }

    @Override
    public List<Role> selectRole() {
        return jurisdiction.selectRole();
    }

    @Override
    public String updateUserTable(List<UserRole> roleId, TmsUser tmsUser) {
        int i = jurisdiction.updateTmsUser(tmsUser);
        int i1 = jurisdiction.delUserRole(tmsUser.getTmsId());
        int i2 = jurisdiction.addUserRole(tmsUser.getTmsId(),roleId);
        return (i>=1&&i1>=1&&i2>=1)?"修改成功":"修改失败";
    }


    @Override
    public List<Menus> getParent() {
        //先查询所有 菜单信息
        List<Menu> departments = jurisdiction.menuAll();
        //工具类  Menus id title children
        List<Menus> menusList = new ArrayList<>();
        for (Menu d : departments) { // 主键id为1 的菜单
            //如果相等为第一级
            if ( d.getMenuMenuid().equals("0")) {  //获取上级菜单为0的为第一级
                Menus menus = new Menus();
                menus.setTitle(d.getMenuname());//第一级名字
                menus.setId(d.getMenuId());
                //Children为集合，调用下面递归方法
                menus.setChildren(getDepartmentList(d.getMenuId())); // 1
                menusList.add(menus);
            }
        }
        return menusList;
    }


    /**
     * @param pid   首次所传为最大父级的id，值为 1
     * @param
     * @return
     * */
    public List<Menus> getDepartmentList(String pid) { //得到值6  //上级菜单没有为 3
        List<Menus> menusList = new ArrayList<>(); //用来存储值
        List<Menu> list = jurisdiction.selectMenuId(pid); // 查询上级菜单为1的  7
        for (Menu d : list) { //  3      4  6
            Menus menus = new Menus();
            menus.setTitle(d.getMenuname());
            menus.setId(d.getMenuId());
            //递归
            menus.setChildren(getDepartmentList(d.getMenuId())); //查询下级菜单为6的
            menusList.add(menus);
        }
        return menusList;
    }

    @Override
    public List<MenusText> getParentText(String tmsId) {
        //先查询所有 菜单信息
        List<Menu> departments = jurisdiction.menuAllTmsId(tmsId);
        //工具类  Menus id title children
        List<MenusText> menusList = new ArrayList<>();
        for (Menu d : departments) { // 主键id为1 的菜单
            //如果相等为第一级
            if ( d.getMenuMenuid().equals("0")) {  //获取上级菜单为0的为第一级
                MenusText menusText = new MenusText();
                menusText.setName(d.getMenuurl());
                menusText.setTitle(d.getMenuname());

                //Children为集合，调用下面递归方法
                menusText.setList(getDepartmentListText(d.getMenuId(),tmsId)); // 1
                menusList.add(menusText);
            }
        }
        return menusList;
    }
    public List<MenusText> getDepartmentListText(String pid,String tmsId) { //得到值6  //上级菜单没有为 3
        List<MenusText> menusList = new ArrayList<>(); //用来存储值
        List<Menu> list = jurisdiction.selectMenuIdTms(pid,tmsId); // 查询上级菜单为1的  7
        for (Menu d : list) { //  3      4  6
            MenusText menus = new MenusText();
            menus.setName(d.getMenuurl());
            menus.setTitle(d.getMenuname());
            //递归
            menus.setList(getDepartmentListText(d.getMenuId(),tmsId)); //查询下级菜单为6的
            menusList.add(menus);
        }
        return menusList;
    }
}
